package com.concurrent.chapter1.concurrent08;

import java.util.concurrent.Exchanger;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MyThread extends Thread {
	private Exchanger<String> exchanger;
	public MyThread(Exchanger<String> exchanger) {
		super();
		this.exchanger = exchanger;
	}
	@Override
	public void run() {
		try {
			System.out.println(exchanger.exchange("test-A", 3, TimeUnit.SECONDS));
			System.out.println("MyThread end");
		} catch (TimeoutException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Exchanger<String> exchanger = new Exchanger<>();
		MyThread myThread = new MyThread(exchanger);
		myThread.start();
		System.out.println("Main end");
	}
}
